package com.example.gulimall.common.utils;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ResponseUtil {

    /**
     *  使用response输出JSON
     * @param response
     * @param resultMap
     */
    public static void out(HttpServletResponse response, Object resultMap){

        ServletOutputStream out = null;
        try {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            out = response.getOutputStream();
            out.write(new Gson().toJson(resultMap).getBytes());
        } catch (Exception e) {
            log.error(e + "输出JSON出错");
        } finally{
            if(out!=null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 拼装Map
     */
    public static Map resultMap(Integer code,String msg,String data){
        HashMap<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("code",code);
        hashMap.put("msg",msg);
        hashMap.put("data",data);
        return hashMap;
    }
    /**
     * 拼装Map
     */
    public static Map resultMap(Integer code,Object msg){
        HashMap<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("code",code);
        hashMap.put("msg",msg);
        return hashMap;
    }
}